package com.bawei.property

import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.common.RouterPath
import com.bawei.common.mvi.BaseMviFragment
import com.bawei.property.adapter.CarApplyListAdapter
import com.bawei.property.databinding.FragmentCarApplyBinding
import com.bawei.property.entity.CarApplyValues
import com.bawei.property.intent.CarApplyViewIntent
import com.bawei.property.uistate.CarApplyUiState
import com.bawei.property.viewmodel.CarApplyViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * 车位申请
 */

@AndroidEntryPoint
class CarApplyFragment : BaseMviFragment<FragmentCarApplyBinding>() {

    private val mViewModel: CarApplyViewModel by activityViewModels()
    private var mCarParkData = mutableListOf<CarApplyValues>()
    private lateinit var mParkListAdapter: CarApplyListAdapter


    companion object {
        @JvmStatic
        fun newInstance() = CarApplyFragment()
    }

    override fun getLayoutId(): Int = R.layout.fragment_car_apply

    override fun initViews() {

        mDataBinding.rvCarSeat.layoutManager = GridLayoutManager(requireContext(), 6, GridLayoutManager.VERTICAL, false)
        mParkListAdapter = CarApplyListAdapter(mCarParkData)
        mParkListAdapter.setItemOnClickListener(object : CarApplyListAdapter.ItemParkOnClickListener{
            override fun onClick(v: View, position: Int) {
                var entity = mCarParkData[position]
                //车位上有车
                if(entity.parkUser > 0){
                    ARouter.getInstance().build(RouterPath.PATH_PARK_DETAIL)
                        .withSerializable("carApplyEntity", entity)
                        .navigation()
                }
                //车位上没有车
                else{
                    showToastMessage("当前车位可以申请")
                }
            }
        })
        mDataBinding.rvCarSeat.adapter = mParkListAdapter
    }

    override suspend fun sendViewIntent() {
        mViewModel.channel.send(CarApplyViewIntent.LoadCarApplyList)
    }


    override suspend fun collectUIState() {
        mViewModel.uiState.collect {
           when(it){
               is CarApplyUiState.LoadCarApplyListSuccess -> showCarApplyList(it.data as List<CarApplyValues>)
               is CarApplyUiState.LoadCarApplyListFail -> showToastMessage(it.msg)
               else -> {}
           }
        }
    }

    private fun showCarApplyList(list: List<CarApplyValues>){
        if(list.isNotEmpty()){
            mCarParkData.clear()
            var newList = mutableListOf<CarApplyValues>()
            //插入每行首列行号数据
            var lineNum = if(list.size % 5 > 0) list.size / 5 + 1 else list.size / 5
            var lastIndex = 0
            for(index in 1 .. lineNum){
                newList.add(CarApplyValues().apply {
                    parkUser = -1
                    parkName = "第${index}排"
                })
                lastIndex = if(index * 5 > list.size) list.size else index * 5
                newList.addAll(list.subList((index - 1) * 5, lastIndex))
            }
            //更新数据
            mCarParkData.addAll(newList)
            mParkListAdapter.notifyDataSetChanged()

        }
    }


}